package com.efast.cafe.portal.controller.api;

import javax.servlet.http.HttpServletRequest;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.api.PortalApiProjectBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectDto;
import com.efast.cafe.portal.bean.api.PortalApiUpdateFaultDefBean;
import com.efast.cafe.portal.service.api.IPortalApiProjectService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

/**
 * <p>
 * Controller
 * </p>
 * <p>
 * Table: portal_api_project - 所属项目定义
 * </p>
 *
 * @since 2017-08-08 11:45:28
 */
@Controller
@RequestMapping("/frame_center/api")
public class PortalApiProjectController extends AbstractController {

	@Autowired
	private IPortalApiProjectService portalApiProjectService;

	

	/**
	 * 新增apiProject
	 * 
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/add")
	public @ResponseBody ResultBean insertApiProject(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		Integer id = null;
		try {
			id = portalApiProjectService.insertApiProject(apiProjectBean);
			result.setObject(id.toString());
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				/* result.setMessage(context.getMessage(ADD_FAILURE_TIP)); */
			}
		}
		return result;

	}

	/**
	 * 修改apiProject
	 * 
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/update")
	public @ResponseBody ResultBean updateApiProject(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiProjectService.updateApiProject(apiProjectBean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 删除apiProject---------》修改isdel字段为1 删除需判断该项目有无系统，有的话不能删除
	 * 
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/delete")
	public @ResponseBody ResultBean deleteApiProject(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiProjectService.deleteApiProject(apiProjectBean);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 项目发布
	 * 
	 * @Date : 2017年8月15日
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/publish")
	public @ResponseBody ResultBean publishApiProject(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiProjectService.publishApiProject(apiProjectBean);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 项目升级
	 * 
	 * @Date : 2017年8月15日
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/upgrade")
	public @ResponseBody ResultBean upgradeApiProject(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiProjectService.upgradeApiProject(apiProjectBean);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 构造树
	 * 
	 * @Date : 2017年8月18日
	 * @param portalApiProjectDto
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/tree")
	public @ResponseBody ResultBean treeApiProject(@RequestBody PortalApiProjectDto portalApiProjectDto, HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			result.setObject(portalApiProjectService.treeApiProject(portalApiProjectDto));
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 切换树 替换（查询除了当前项目版本之外的该项目的所有版本）
	 * 
	 * @Date : 2017年8月18日
	 * @param portalApiProjectDto
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/queryProjectByIdAndUUid")
	public @ResponseBody ResultBean queryProjectByIdAndUUid(@RequestBody PortalApiProjectBean apiProjectBean, HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			result.setObject(portalApiProjectService.queryProjectByIdAndUUid(apiProjectBean));
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 切换
	 * 
	 * @Date : 2017年8月30日
	 * @param apiProjectBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/project/updateDefaultDef")
	public @ResponseBody ResultBean updateDefaultDef(@RequestBody PortalApiUpdateFaultDefBean updateFaultDefBean, HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiProjectService.updateDefaultDef(updateFaultDefBean);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

}